'use strict';
const fs = require('fs');
const exec = require('child_process').exec;
const chalk = require('chalk');
const ora = require('ora');
const rimraf = require('rimraf');

module.exports = ({ git, npm, path }) => {
    return new Promise((resolve, reject) => {
        // 开始下载
        const _init = () => {
            if(npm) {
                // 判断是否为npm
            } else if(git) {
                // 判断是否为git
                const spinner = ora(`  正在下载${git}`).start();
                // 下载更新远程仓库
                exec(`git clone ${git} ${path}`, (error, stdout, stderr) => {
                   if(error) {
                       console.log(chalk.bgRed(stderr));
                       process.exit();
                   } else {
                       // 下载成功
                       spinner.stop();
                       resolve();
                   }
               });
            } else {
                resolve();
            }
        };
        // 判断文件是否存在
        fs.exists(path, exists => {
            if(exists) {
                rimraf(path, err => {
                    if (err) throw err;
                    _init();
                });
            } else {
                _init();
            }
        });
    });
};